package com.aidong.dynamic.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.dynamic.biz.TrnPraiseBiz;
import com.aidong.dynamic.entity.TrnDynamicContent;
import com.aidong.util.ReadPropertiesUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 运动圈点赞action
 * 借助于运动圈实体类
 * @author java
 * @version 1.0    
 *  2015.6.4
 */
@ParentPackage("json")
@Namespace("/")
public class TrnPraiseAction implements ModelDriven<TrnDynamicContent> {

	private TrnDynamicContent dynamicContent; // 接收客户端数据
	
	@Override
	public TrnDynamicContent getModel() {
		if(dynamicContent == null){
			dynamicContent = new TrnDynamicContent();
		}
		return dynamicContent;
	}
	
	private TrnPraiseBiz trnPraiseBiz; // 点赞service
	
	/**
	 * 点赞service
	 * @param trnPraiseBiz
	 */
	@Autowired
	public void setTrnPraiseBiz(TrnPraiseBiz trnPraiseBiz) {
		this.trnPraiseBiz = trnPraiseBiz;
	}

	Map<String, Object> map; // 返回的json数据
	
	/**
	 * 返回的json数据
	 * @return
	 */
	@JSON
	public Map<String, Object> getMap() {
		return map;
	}

	/**
	 * 新增点赞
	 * @return
	 */
	@Action(value="addtrnpraise",results={@Result(name="succ",type="json",params={"root","map"})})
	public String addTrnPraise(){
		map = new HashMap<String, Object>();
		try {
			if(dynamicContent.getIdongId() > 0 && dynamicContent.getDynamicId() > 0
					&& (dynamicContent.getStatus() != null && !"".equals(dynamicContent.getStatus()))) {
				// 先判断登陆用户是否对此条动态已点赞 
				Map<String, Object> countMap = new HashMap<String, Object>();
				countMap.put("idongId", dynamicContent.getIdongId());
				countMap.put("dynamicId", dynamicContent.getDynamicId());
				int count = trnPraiseBiz.getCountPraise(countMap);
				if(count > 0){ // 已点赞，修改点赞状态
					Map<String, Object> statusMap = new HashMap<String, Object>();
					statusMap.put("idongId", dynamicContent.getIdongId());
					statusMap.put("dynamicId", dynamicContent.getDynamicId());
					statusMap.put("status", dynamicContent.getStatus());
					trnPraiseBiz.editCountPraiseStatus(statusMap);
					List<Map<String, Object>> praiseMap = trnPraiseBiz.getPraiseByDynamicId(dynamicContent.getDynamicId());
					if(praiseMap.size() > 0){
						for (Map<String, Object> praise : praiseMap) {
							String headUrl = praise.get("headUrl") + "";
							if(!"".equals(headUrl) && headUrl != null){
								praise.put("headUrl", ReadPropertiesUtil.getIMGREAD(headUrl));
							}else{
								praise.put("headUrl", "");
							}
						}
					}
					map.put("code", 1);
					map.put("result", praiseMap);
					return "succ";
				}else{ // 未点赞，新增一条点赞记录
					Map<String, Object> paraMap = new HashMap<String, Object>();
					paraMap.put("currentIdongId", dynamicContent.getIdongId());
					paraMap.put("currentRecipientId", 0);
					paraMap.put("dynamicId", dynamicContent.getDynamicId());
					trnPraiseBiz.addTrnPraise(paraMap);
					List<Map<String, Object>> praiseMap = trnPraiseBiz.getPraiseByDynamicId(dynamicContent.getDynamicId());
					if(praiseMap.size() > 0){
						for (Map<String, Object> praise : praiseMap) {
							String headUrl = praise.get("headUrl") + "";
							if(!"".equals(headUrl) && headUrl != null){
								praise.put("headUrl", ReadPropertiesUtil.getIMGREAD(headUrl));
							}else{
								praise.put("headUrl", "");
							}
						}
					}
					map.put("code", 1);
					map.put("result", praiseMap);
					return "succ";
				}
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
}
